package com.cpecc.quartz;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.cpecc.dao.system.EmailDao;
import com.cpecc.dao.system.HuanJieDao;
import com.cpecc.dao.system.RootDao;
import com.cpecc.dao.system.ToDoDao;
import com.cpecc.dao.system.UserDao;
import com.cpecc.dao.system.YouJianDao;
import com.cpecc.dao.wuzi.ShenQingDao;
import com.cpecc.dao.wuzi.ShenQingJLDao;
import com.cpecc.model.system.Email;
import com.cpecc.model.system.ToDo;
import com.cpecc.model.system.User;
import com.cpecc.model.system.YouJian;
import com.cpecc.model.wuzi.ShenQingJL;
import com.cpecc.model.wuzi.WuZi;

@Service
public class AutoSureSchedule{
	
	@Autowired
	private ShenQingJLDao shenQingJLDao;
	
	@Autowired
	private ShenQingDao shenQingDao;
	
	@Autowired
	private YouJianDao youJianDao;
	
	@Autowired
	private RootDao rootDao;
	
	@Autowired
	private EmailDao emailDao;
	
	@Autowired
	private HuanJieDao huanJieDao;
	
	@Autowired
	private ToDoDao toDoDao;
	
	@Autowired
	private UserDao userDao;
	
	public void autoSure(){
		String root=rootDao.getRoot2();
		Calendar cal=Calendar.getInstance();
		cal.add(Calendar.DATE, -7);
		Date date=cal.getTime();
		List<String> shenQingIdList=shenQingJLDao.getWeiJieShou(date);
		final Email email=emailDao.getEmail2();
		for(String sqId:shenQingIdList){
			try {
				shenQingDao.cover1(sqId);
				
				ShenQingJL shenQingJL=new ShenQingJL();
				shenQingJL.setCaoZuoRQ(new Date());
				shenQingJL.setHuanJie(huanJieDao.get1(107));
				shenQingJL.setPiFuLX("自动确认");
				shenQingJL.setPiFuNR("使用人验收");
				shenQingJL.setShenQing(shenQingDao.get1(sqId));
				shenQingJL.setUser(shenQingDao.getDaiJieShouUser(sqId));
				shenQingJLDao.save1(shenQingJL);
				
				toDoDao.deleteListSQ(shenQingDao.get1(sqId));
				for(User user1:userDao.getGZGLY1()){
					ToDo toDo = new ToDo();
					toDo.setCreateDate(new Date());
					toDo.setHuanJie(huanJieDao.get1(108));
					toDo.setShenQing(shenQingDao.get1(sqId));
					toDo.setTask("使用人已验收");
					toDo.setType("申请采购");
					toDo.setUser(user1);
					toDoDao.save1(toDo);
				}
				
				YouJian youJian=youJianDao.get2(108);
				String content=youJian.getContent();
				User user=shenQingDao.getDaiJieShouUser(sqId);
				WuZi wuZi=shenQingDao.getDaiJieShouWuZi(sqId);
				String format="yyyy-MM-dd";
				SimpleDateFormat sdf=new SimpleDateFormat(format);
				String gouMaiRQ=sdf.format(wuZi.getGouMaiRQ());
				content=content.replace("{购买日期}", gouMaiRQ);
				content=content.replace("{资产编号}", wuZi.getZiChanBH());
				content=content.replace("{物资名称}", wuZi.getWuZiMC());
				content=content.replace("{品牌}", wuZi.getPinPai());
				content=content.replace("{规格型号}", wuZi.getGuiGeXH());
				content=content.replace("{使用部门}", shenQingDao.getDaiJieShouDep(sqId));
				content=content.replace("{使用人}", user.getName());
				content=content.replaceAll(" ", "&nbsp;");
				content=content.replaceAll("\n", "<br>");
				String access_url="<a href=\""+root+"/youjian/access.do?userId="+user.getId()+"&huanJie="+108+"\">进入</a>";
				content=content.replace("{access_url}", access_url);
				
				
				String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
				Properties props = System.getProperties();
				props.setProperty("mail.smtp.host", email.getMailServerHost());
				props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
				props.setProperty("mail.smtp.ssl.enable", "true");
				props.setProperty("mail.smtp.socketFactory.fallback", "false");
				props.setProperty("mail.smtp.port", email.getMailServerPort());
				props.setProperty("mail.smtp.socketFactory.port", email.getMailServerPort());
				props.put("mail.smtp.auth", "true");
				Session session = Session.getDefaultInstance(props,
						new Authenticator() {
							protected PasswordAuthentication getPasswordAuthentication() {
								return new PasswordAuthentication(email.getUserName(), email.getPassword());
							}
						});
				try {
					Multipart mainPart = new MimeMultipart();
					BodyPart html = new MimeBodyPart();
					// 设置HTML内容
					html.setContent(content, "text/html; charset=utf-8");
					mainPart.addBodyPart(html);
					Message msg = new MimeMessage(session);
					msg.setFrom(new InternetAddress(email.getUserName()));
					msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(user.getEmail(), false));
					msg.setSubject(youJian.getTitle());
					msg.setContent(mainPart);
					msg.setSentDate(new Date());
					Transport.send(msg);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}